package com.guyou.guguxinge;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import com.cbs.android.component.applicationutils.network.ErrorResponse;
import com.cbs.android.component.log.L;
import com.cbs.android.component.toast.Toast;
import com.cbs.android.module.user.user.UserModule;
import com.cbs.android.module.user.user.entity.User;
import com.cbs.android.module.user.user.handler.ModuleHandler;
import com.cbs.android.module.user.user.handler.UserChangeHandler;
import com.google.gson.Gson;

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void login(View view) {
        UserModule.login("13382850839", "456", null, new DefaultHandler<User>() {
            //        UserModule.login("13382850839", "123456", null, new DefaultHandler<User>() {
            @Override
            public void onSuccess(User entity) {
                Toast.show(entity.getNickName());
            }
        });
    }

    public void loginwithhandler(View view) {
        UserModule.login("13382850839", "123456", new UserChangeHandler() {
            @Override
            public String shouldChange() {
                return "无法切换";
//                return null;
            }

            @Override
            public void onChanged(User user) {
                Toast.show("切换完成");
            }
        }, new DefaultHandler<User>() {
            @Override
            public void onSuccess(User entity) {
                Toast.show(entity.getNickName());
            }
        });
    }

    public void logout(View view) {
        UserModule.logout(new SimpleDefaultHandler());
    }

    public void logoutwithhandler(View view) {
        UserModule.logout(new UserChangeHandler() {
            @Override
            public String shouldChange() {
//                return "无法切换";
                return null;
            }

            @Override
            public void onChanged(User user) {
                Toast.show("切换完成");
            }
        }, new SimpleDefaultHandler());
    }

    private abstract class DefaultHandler<T> implements ModuleHandler<T> {
        @Override
        public void onFailure(int code, String message) {
            ErrorResponse errorResponse = new Gson().fromJson(message, ErrorResponse.class);
            Toast.show("code: " + code + "\nsubcode: " + errorResponse.getErrorNo() + "\nmessage: " + errorResponse.getErrorMsg());
        }

        @Override
        public void onException(Exception e) {
            Toast.show("exception");
            L.e(TAG, "", e);
        }
    }

    private class SimpleDefaultHandler extends DefaultHandler {
        @Override
        public void onSuccess(Object entity) {
            Toast.show("success");
        }
    }
}
